package com.todo.content.application.handler.changeStatus;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.common.enums.DetectionRiskLevelEnum;
import com.todo.biz.service.api.request.ContentDetectionRequest;
import com.todo.biz.service.api.response.BaseExamineResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.content.application.entity.bo.ContentDetailBO;
import com.todo.content.application.entity.context.ContentStatusContext;
import com.todo.content.application.remote.BizExamineRemoteService;
import com.todo.content.application.service.ContentInfoService;
import com.todo.content.common.enums.ContentStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/12/12 19:52
 * @description
 */
@Slf4j
@Component
public class PostHandler extends AbstractContentStatusHandler {

    @Resource
    private ContentInfoService contentInfoService;
    @Resource
    private BizExamineRemoteService bizExamineRemoteService;

    @Override
    protected Boolean doStatusHandle(ContentStatusContext context) {
        Long contentId = context.getContentId();
        ContentDetailBO contentDetailBO = contentInfoService.queryContentDetailById(contentId);
        if (contentDetailBO == null) {
            log.error("PostHandler#doStatusHandle 发布状态处理完成，数据为空，默认执行成功 context:{}", JSONObject.toJSONString(context));
            return true;
        }
        if (!contentDetailBO.getContentInfoBO().getStatus().equals(context.getCurrentStatus())) {
            log.error("PostHandler#doStatusHandle 当前数据状态与发送消息状态不一致，无法处理当前状态下的数据情况 context:{}，当前状态 :{}", JSONObject.toJSONString(context), contentDetailBO.getContentInfoBO().getStatus());
            return true;
        }
        ContentDetectionRequest request = new ContentDetectionRequest();
        request.setBizId(contentId);
        request.setSceneEnum(BusinessSceneEnum.CONTENT);
        request.setContent(contentDetailBO.getContentInfoBO().getContent());
        BizResponse<BaseExamineResponse> bizResponse = bizExamineRemoteService.contentDetection(request);
        if (bizResponse == null || !bizResponse.isSuccess() || bizResponse.getData() == null) {
            //审查数据为空，默认转人工操作
            Boolean updated = contentInfoService.updateContentStatus(contentId, ContentStatusEnum.POSTED, ContentStatusEnum.AUTO_EXAMINE_FAILED);
            return updated;
        }
        BaseExamineResponse baseExamineResponse = bizResponse.getData();
        if (DetectionRiskLevelEnum.MEDIUM.equals(baseExamineResponse.getReviewLevelEnum())) {
            //审查数据为中等，建议
            Boolean updated = contentInfoService.updateContentStatus(contentId, ContentStatusEnum.POSTED, ContentStatusEnum.AUTO_EXAMINE_FAILED);
            return updated;
        }
        if (DetectionRiskLevelEnum.HIGH.equals(baseExamineResponse.getReviewLevelEnum())) {
            log.warn("PostHandler#doStatusHandle 审核高危，后续建议人工介入观察 结果:{}", JSONObject.toJSONString(baseExamineResponse));
            return contentInfoService.updateContentStatus(contentId, ContentStatusEnum.POSTED, ContentStatusEnum.DISCARD);
        }
        Boolean updated = contentInfoService.updateContentStatus(contentId, ContentStatusEnum.POSTED, ContentStatusEnum.PUBLISHED);
        return updated;
    }

    @Override
    protected boolean match(ContentStatusContext context) {
        if (context == null || context.getContentId() == null || context.getCurrentStatus() == null) {
            log.error("PostHandler#match 前置数据匹配失败，数据缺失或不存在 context:{}", JSONObject.toJSONString(context));
            return false;
        }
        return true;
    }

    @Override
    protected ContentStatusEnum doGetDealStatus() {
        return ContentStatusEnum.POSTED;
    }
}
